package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityReject;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityRejectDao;
import cc.comac.mom.entity.QualityRejectEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
public class QualityRejectDataHandler extends ServiceImpl<QualityRejectDao, QualityRejectEntity>{

	
	@Autowired
	private QualityRejectDao qualityRejectDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean insert(QualityReject obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		if(obj.getInnerData() == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		if(obj.getInnerData().getFormNo() == null || "".equals(obj.getInnerData().getFormNo())) {
			resultBean.setErrorMessge("表单编号不能为空");
			return resultBean;
		}
		if(obj.getInnerData().getRejectType() == null || "".equals(obj.getInnerData().getRejectType())) {
			resultBean.setErrorMessge("驳回类型不能为空");
			return resultBean;
		}
		baseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		int insert = qualityRejectDao.insert(obj.getInnerData());
		if(insert <1) {
			resultBean.setErrorMessge("驳回信息插入失败");
			return resultBean;
		}
		return resultBean;
		
	}
	
	public ResultBean insertBatch(List<QualityReject> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		ArrayList<QualityRejectEntity> listEntity = new ArrayList<QualityRejectEntity>();
		for(QualityReject item :list) {
			QualityRejectEntity innerData = item.getInnerData();
			if(innerData == null) {
				resultBean.setErrorMessge("数据为空");
				return resultBean;
			}
			if(innerData.getFormNo() == null || "".equals(innerData.getFormNo())) {
				resultBean.setErrorMessge("表单编号不能为空");
				return resultBean;
			}
			if(innerData.getRejectType() == null || "".equals(innerData.getRejectType())) {
				resultBean.setErrorMessge("驳回类型不能为空");
				return resultBean;
			}
			listEntity.add(innerData);
		}
		for(QualityRejectEntity item:listEntity) {
			baseEntityDataUtil.setBaseEntityData(item);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(!saveBatch) {
			resultBean.setErrorMessge("插入驳回信息失败");
			return resultBean;
		}
		return resultBean;
	}
	
	
	public ResultBean deleteBatch(List<QualityReject> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		ArrayList<QualityRejectEntity> listEntity = new ArrayList<QualityRejectEntity>();
		for(QualityReject item :list) {
			QualityRejectEntity innerData = item.getInnerData();
			if(innerData == null) {
				resultBean.setErrorMessge("数据为空");
				return resultBean;
			}
			QualityRejectEntity rejectEntity = qualityRejectDao.selectById(innerData.getId());
			if(rejectEntity == null) {
				resultBean.setErrorMessge("主键id为"+innerData.getId()+"的驳回信息不存在");
				return resultBean;	
			}
			listEntity.add(rejectEntity);
		}
		for(QualityRejectEntity item:listEntity) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean saveBatch = this.updateBatchById(listEntity);
		if(!saveBatch) {
			resultBean.setErrorMessge("批量删除驳回信息失败");
			return resultBean;
		}
		return resultBean;
	}
	
	
	public LimitPageBean<QualityRejectEntity> selectByCondition(QualityReject obj) {
    	QueryWrapper<QualityRejectEntity> queryWrapper = new QueryWrapper<QualityRejectEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj.getPageSize() == 0) {
    		obj.setPageSize(10);
    	}
		if(obj != null && obj.getInnerData() != null) {		
    		if(obj.getInnerData().getFormNo() != null && !"".equals(obj.getInnerData().getFormNo())) {
    			queryWrapper.like("form_no",obj.getInnerData().getFormNo());
    		}
    		if(obj.getInnerData().getRejectType() != null && !"".equals(obj.getInnerData().getRejectType())) {
    			queryWrapper.like("reject_type",obj.getInnerData().getRejectType());
    		}
    		if(obj.getInnerData().getRejectBeforeStatus() != null && !"".equals(obj.getInnerData().getRejectBeforeStatus())) {
    			queryWrapper.like("reject_before_status",obj.getInnerData().getRejectBeforeStatus());
    		}
    		if(obj.getInnerData().getRejectEndStatus()!= null && !"".equals(obj.getInnerData().getRejectEndStatus())) {
    			queryWrapper.like("reject_end_status",obj.getInnerData().getRejectEndStatus());
    		}
		}
		Page<QualityRejectEntity> page = new Page<>(obj.getPageNum(),obj.getPageSize(),true);
		IPage<QualityRejectEntity> iPage = qualityRejectDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<QualityRejectEntity> limitPageBean = (LimitPageBean<QualityRejectEntity>) LimitPageBean.of(iPage);
		return limitPageBean;
		
	}
	
}
